#!/bin/bash 

declare -a MenuList
MenuList=( "POST A DPI RULE"
		   "DUMP RULE CONFIGFILE TO LOCAL"
		   "LOCAL FILE LIST"
		   "QUIT")

declare -a chooseArray
chooseArray=(1 2 3 4)

declare -a  MAINURL
MAINURL="http://192.168.100.221:9000/"


#
#判断是否有安装curl
function checkLocalIsNotInstallCurl(){
	local res=""
	res=`ls /bin/ | grep -w "curl"`
	if [ "$res" != "" ]; then  
		echo "[INFO]CURRENT PC HAS INSTALLED CURL TOOL"
		return "1"
	else 
		echo "[ERROR]CURRENT PC HAS NOT INSTALLED CURL TOOL, PLEASE INSTALL FIRSTLY"
		return "0"
	fi 
}

#
#
#检查文件是否存在
function checkFileIsNotExit(){
	local filename=$1
	echo "filename:" $filename
	if [ -o $1 ];then 
		echo "[ERROR]GET UNAVALIABLE ARGS IN LINE"
		return "0"
	fi 
	if [ ! -f $filename ];then 
		echo "[ERROR]YOU HAS INPUT FILE:$filename IS NOT EXIST!"
		return "0"
	fi 
	return "1"
}

#
#
function localFileList()
{
	echo "========================================================================"
	local result=""
	result=`ls -l | sort`
	echo "$result"
	echo "========================================================================"
	read -p"[INFO]INPUT UPLOAD FILE YOU WANTED:" fileName
	checkFileIsNotExit $fileName
	if [ "$?" -eq "0" ];then 
		return "0"
	fi 
	#凭借URL
	echo "[INFO]fileName=$fileName"
	UPLOADURL="$MAINURL""/action=upload?""uploadFile=""$fileName"
	result=`curl "$UPLOADURL"`
	echo "[INFO]$result"
	sleep 4
}


#
#主菜单
function userMenu(){
	clear
	echo "[INFO]CHOOSE OPERATION YOU SELECTED:"
	while ((1)) 
	do
		clear
		select var in "${MenuList[@]}"
		do
			if ! echo ${chooseArray[@]} | grep -q $REPLY; then 
				echo "[ERROR]PLEASE ENTER [1-4]."
				continue 
			fi
			echo "[INFO]YOU HAVE SELECTED \"$var\""
			break;
		done 
		case "$var" in 
			"POST A DPI RULE")

			;;
			"DUMP RULE CONFIGFILE TO LOCAL") #download 
			    #对比和之前备份的差距;

			;;
			"LOCAL FILE LIST")  #upload 
			localFileList
			;;
			"QUIT")
			exit 0
			;;
		esac
	done 
}


#主函数
function  main(){
	checkLocalIsNotInstallCurl 
	if [ "$?" == "1" ]; then
		userMenu 

	fi 
}


main 
